package com.shuma.user.controller;

import com.shuma.user.common.BaseResponse;
import com.shuma.user.common.ErrorCode;
import com.shuma.user.common.ResultUtils;
import com.shuma.user.exception.BusinessException;
import com.shuma.user.model.user.UserForgetPassWord;
import com.shuma.user.model.user.UserLoginRequest;
import com.shuma.user.model.user.UserRegister;
import com.shuma.user.pojo.User;
import com.shuma.user.service.UserService;
import com.shuma.user.service.impl.UserServiceImpl;
import com.shuma.user.utils.RegexUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    private static final Logger LOG = LoggerFactory.getLogger(UserServiceImpl.class);
//        StpUtil.checkRole("admin1");

    /**
     * 用户注册
     * @param userRegister
     * @return Boolean
     */
    @PostMapping("/register")
    public BaseResponse<Boolean> userRegister(@RequestBody UserRegister userRegister)
    {
        if(userRegister==null)
        {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String userName = userRegister.getUserName();
        String password = userRegister.getUserPassword();
        String checkpassword = userRegister.getCheckPassword();
        String phoneNumber = userRegister.getPhoneNumber();
        String captcha = userRegister.getCaptcha();
        if(StringUtils.isAnyBlank(userName,password,checkpassword,phoneNumber,captcha))
        {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean l = userService.userRegister(userName, password, checkpassword,phoneNumber,captcha);
        return ResultUtils.success(l);
    }

    /**
     * 发送验证码
     * @param phone
     * @return String
     */
    @PostMapping("/code")
    public BaseResponse<String> sendCode(@RequestParam("phone") String phone)
    {
        //1.校验手机号
        if (RegexUtils.isPhoneInvalid(phone)) {
            //2.如果不符合则返回报错信息
            LOG.info("手机号错误");
            return ResultUtils.error(ErrorCode.PARAMS_ERROR,"手机号有误");
        }
        String code = userService.sendCode(phone);
        return ResultUtils.success(code);
    }

    /**
     * 用户登录
     * @param userLoginRequest
     * @return User
     */
    @PostMapping("/passwordLogin")
    public BaseResponse<User> login(@RequestBody UserLoginRequest userLoginRequest) {

        if (userLoginRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String userName = userLoginRequest.getUserName();
        String userPassword = userLoginRequest.getUserPassword();
        if (StringUtils.isAnyBlank(userName, userPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User user = userService.userLogin(userName, userPassword);
        return ResultUtils.success(user);
    }

    /**
     * 忘记密码
     * @param userForgetPassWord
     * @return Boolean
     */
    @PostMapping("/forgetPassWord")
    public BaseResponse<Boolean> userForgetPassWord(@RequestBody UserForgetPassWord userForgetPassWord)
    {
        if(userForgetPassWord==null)
        {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String userName = userForgetPassWord.getUserName();
        String password = userForgetPassWord.getUserPassword();
        String checkpassword = userForgetPassWord.getCheckPassword();
        String phoneNumber = userForgetPassWord.getPhoneNumber();
        String captcha = userForgetPassWord.getCaptcha();
        if(StringUtils.isAnyBlank(userName,password,checkpassword,phoneNumber,captcha))
        {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Boolean update = userService.userForgetPassWord(userName, password, checkpassword,phoneNumber,captcha);
        return ResultUtils.success(update);
    }

}
